/*
 * @(#)ServiceLocator.java 1.0 2005-2-26
 * Copyright 2005 VandaGroup, Inc. All rights reserved.
 */
package com.vanda.db.pk;
import java.sql.Date;
import com.vanda.db.SQLAccessor;
import com.vanda.exception.VandaException;
/**
 * 类说明: <br>
 * 通用主键工具类 - 日期主键类 <br>
 * 
 * @author 蒋勇 <br>
 * @version Version 1.00 <br>
 */
public class DateKeyInfo {
    /** 主键的名字 */
    private String keyName;
    /** 时间 */
    private Date now;

    /**
     * 构造函数
     */
    public DateKeyInfo(String keyName, Date now) throws VandaException {
        this.keyName = keyName;
        this.now = now;
    }

    /**
     * 取值方法，提供键的当前值,如果当前键值大于最大值,则重数据库取最新的键值返回;
     * 
     * @param con
     * @return
     * @author:蒋勇
     */
    public long getNextKey(SQLAccessor accessor, Date now)
            throws VandaException {
        try {
            this.now = now;
            long keyValue;
            int[] counts = accessor.executeName("pk:updateDateKey", this);
            if (counts[0] == 0) {
                accessor.executeName("pk:createDateKey", this);
                keyValue = 1;
            } else {
                KeyValue key = (KeyValue) accessor.findName("pk:getDateKey",
                        this);
                keyValue = key.getKeyValue();
            }
            return keyValue;
        } catch (Exception e) {
            e.printStackTrace();
            throw new VandaException(e);
        }
    }

    /**
     * 方法说明: 最大值
     * 
     * @return
     * @author wkf3915 2005-8-10
     */
    public String getKeyName() {
        return keyName;
    }

    /**
     * 方法说明: 时间
     * 
     * @return
     * @author wkf3915 2005-8-10
     */
    public Date getNow() {
        return now;
    }
}